package com.ygh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygh.mapper.CarInfoMapper;
import com.ygh.mapper.VipUserMapper;
import com.ygh.pojo.CarInfo;
import com.ygh.pojo.VipUser;
import com.ygh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CarInfoServiceImpl implements CarInfoService{
    @Autowired
    private CarInfoMapper carInfoMapper;
    @Autowired
    private VipUserMapper vipUserMapper;

    private String vipToken;


    @Override
    public List<CarInfo> finVip(String vipToken) {
        this.vipToken=vipToken;


        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vip_token", vipToken);
        VipUser vipUser = vipUserMapper.selectOne(queryWrapper);

        String carId = vipUser.getCarId();
        QueryWrapper<CarInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("car_id",carId);
        List<CarInfo> list = carInfoMapper.selectList(queryWrapper1);
        if(list.isEmpty()){
            return null;
        }
        Date date = new Date();
        long time1 = date.getTime();
        Date inTimes =list.get(0).getInTimes();
        if (inTimes==null){
            return list;
        }
        long time2 = inTimes.getTime();
        long time=time1-time2;

        list.get(0).setPrice(time*3);

        carInfoMapper.update(list.get(0),queryWrapper1);
        return list;
    }

    @Override
    public PageResult findallcar(PageResult pageResult) {
        IPage<CarInfo> page= new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        page = carInfoMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<CarInfo> list = page.getRecords();

        return pageResult.setTotal(total).setRows(list);
    }

    @Override
    public CarInfo updateStatus(CarInfo carInfo) {
        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vip_token", this.vipToken);
        VipUser vipUser = vipUserMapper.selectOne(queryWrapper);
        if(carInfo.getStatus()){
            String carId = vipUser.getCarId();
            carInfo.setCarId(carId);
        }else {
            carInfo.setCarId("");
        }


        carInfoMapper.updateById(carInfo);
        return carInfo;
    }

    @Override
    public List<CarInfo> findmystatus(String carId) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", carId);
        List<CarInfo> list = carInfoMapper.selectList(queryWrapper);
        return list;
    }


}
